$(function () {
    var defaultColumns = initColumn();
    var searchParams = {};
    vm.table = new BSTable('menuTable', "/platform/banner/list", defaultColumns,searchParams);
    vm.table.setPaginationType("server");
    vm.table.init();
    $('#bannerImage').click(function (e) {
        $('#fileInput').trigger('click');
    });
    $("#fileInput").change(function () {
        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
        if (objUrl != null) {
            $("#bannerImage").attr("src", objUrl);
            vm.banner.bannerImg = objUrl;
        }
    });
    vm.loadApps();
});
function initColumn () {
    return [
        {field: 'selectItem', radio: true},
        {title: '标题', field: 'bannerTitle', align: 'center', valign: 'middle',width:'25%'},
        {title: '描述', field: 'bannerDesc', align: 'center', valign: 'middle',width:'25%'},
        {title: '图片', field: 'bannerImg', align: 'center', valign: 'middle',width:'25%',formatter:function (value) {
            return '<img src="'+value+'" style="height: 30px;width: auto;">';
        }},
        {title: '分组', field: 'bannerTag', align: 'center', valign: 'middle',width:'25%'},
    ];
};

var vm = new Vue({
    el:'#app',
    data:{
        showList:true,
        isUpdate:false,
        banner:{},
        title:'',
        table:null,
        apps:[]
    },
    methods:{
        loadApps:function () {
            $.getJSON('/platform/app/apps',function (result) {
                if (result.code == 200){
                    vm.apps = result.data;
                }else
                    alert(result.msg);
            });
        },
        add:function () {
            vm.banner = {};
            vm.isUpdate = false;
            vm.showList = false;
            vm.title = '添加Banner';
        },
        update:function () {
            var data = $.getSelectData('menuTable');
            if (!data)
                return;
            vm.isUpdate = true;
            vm.showList = false;
            vm.banner = data;
            vm.title = '修改信息';
        },
        del:function () {
            var data = $.getSelectData('menuTable');
            if (!data)
                return;
            window.confirm('确定要删除这条数据吗？',function () {
                $.getJSON('/platform/banner/delete/'+data.bannerId,function (result) {
                    if (result.code == 200){
                        alert('操作成功');
                        vm.table.refresh();
                    }else
                        alert(result.msg);
                });
            });
        },
        saveOrUpdate:function () {
            var urlStr = vm.isUpdate ? '/platform/banner/update':'/platform/banner/add';
            var files = $('#fileInput').prop('files');
            if (!vm.isUpdate && files.length == 0){
                alert('请选择一张图片');return;
            }
            Vue.delete(this.banner,'bannerImg');
            $.ajaxFileUpload({
                url:urlStr,
                type:'POST',
                dataType:'JSON',
                data: JSON.parse(JSON.stringify(vm.banner)),
                contentType: "multipart/form-data",
                secureuri: false,
                fileElementId: 'fileInput',
                dataType: 'json',
                success:function (result) {
                    if (result.code == 200){
                        alert('操作成功',function () {
                            vm.showList = true;
                            vm.table.refresh();
                        });
                    }else
                        alert(result.msg);
                },
                complete: function () {
                    $('#fileInput').replaceWith('<input type="file" style="display: none;" accept="image/*" name="bannerImg" id="fileInput" class="form-control" placeholder="file" />');
                    $('#fileInput').change(function () {
                        var objUrl = getObjectURL(this.files[0]); //获取图片的路径，该路径不是图片在本地的路径
                        if (objUrl != null) {
                            $("#bannerImage").attr("src", objUrl);
                            vm.banner.bannerImg = objUrl;
                        }
                    });
                },
                error:function (e) {
                    console.error("request error",e);
                    alert('请求失败');
                }
            })
        },
        cancel:function () {
            vm.showList = true;
        }
    }
});